import shutil

from fastapi import FastAPI, APIRouter, Request, Depends
from json.decoder import JSONDecodeError
import sys 
sys.path.append('..')
from utils import user as F
from utils.http_response import HttpResponse 
from utils.db import engine
from fastapi import File,UploadFile,Form
import uuid
import os
project_dir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

router = APIRouter(prefix="/user",tags=["user"])


@router.post("/login")
async def login(request: Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await request.json()
    except Exception as e:
        resp.message = "error"
        resp.code = 500
        return resp
    data = F.login(engine,body)
    if data is None:
        resp.message = "error"
        resp.code = 500
        return resp
    resp.data = {"data":data,"token":body['username']}
    return resp



@router.post("/get_one")
async def get_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.select_by_col(engine,body)
    return resp

@router.post("/update_one")
async def update_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.update_one(engine,body["cond"],body["item"])  
    return resp 

@router.post("/delete_one")
async def delete_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.delete_one(engine,body)  
    return resp 



@router.post("/add_one")
async def add_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.add_one(engine,body)  
    return resp 



@router.post("/set_group")
async def set_group(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
        resp.data = F.set_group(engine,body)
    except Exception as e:
        resp.message = e
        resp.code = 500
    return resp

@router.post("/upload")
async def upload(file:UploadFile=File(...)):
    resp = HttpResponse(code=200,message="success",data=[])
    suffix = file.filename.split(".")[-1]
    filename = uuid.uuid4().hex + "." + suffix
    with open(project_dir+"/static/"+filename, "wb") as f:
        shutil.copyfileobj(file.file, f)
    resp.data = "http://127.0.0.1:8000/static/"+filename
    return resp


